package fr.castorhoz.isi_connectivity_widget.GPSWidget;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.GpsStatus;
import android.location.LocationManager;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;
import fr.castorhoz.isi_connectivity_widget.ConnButton;
import fr.castorhoz.isi_connectivity_widget.R;

public class GPSButton extends ConnButton {

	private static GpsStatus.Listener gpsListener = new GpsStatus.Listener() {

		public void onGpsStatusChanged(int event) {
			// TODO Auto-generated method stub
			switch (event) {
			case GpsStatus.GPS_EVENT_STARTED:
				Log.i(this.getClass().toString(), "GPS EVENT STARTED");
				break;
			case GpsStatus.GPS_EVENT_STOPPED:
				Log.i(this.getClass().toString(), "GPS_EVENT_STOPPED");
				break;
			}
		}
	};
	

	public GPSButton(Context context) {
		// //RECEIVERS//////////////////////////////////////////////
		if (receiver == null)
			receiver = new GPSBroadcastReceiver(this);
		IntentFilter filter = new IntentFilter(LocationManager.GPS_PROVIDER);
		context.getApplicationContext().registerReceiver(receiver, filter);
		// //////////////////////////////////////////////////////////

		
		getLocationManager(context).addGpsStatusListener(gpsListener);
	}

	// ///////////////TODO
	@Override
	public void enableNetwork(Context context) {
		String provider = Settings.Secure.getString(
				context.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (provider.contains("gps")) {
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			context.getApplicationContext().sendBroadcast(poke);

		}

	}

	@Override
	public void disableNetwork(Context context) {
		String provider = Settings.Secure.getString(
				context.getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (provider.contains("gps")) {
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			context.getApplicationContext().sendBroadcast(poke);

		}
	}

	private LocationManager getLocationManager(Context context) {
		return (LocationManager) context.getApplicationContext()
				.getSystemService(Context.LOCATION_SERVICE);
	}

	@Override
	public String getAction() {
		return "fr.imberle.connectivityWidget.CLICK_GPS";
	}

	@Override
	public int getSelectorOff() {
		return R.drawable.gps_selector_off;
	}

	@Override
	public int getSelectorOn() {
		return R.drawable.gps_selector_on;
	}

	@Override
	public int getSelectorUnknown() {
		// TODO Auto-generated method stub
		return R.drawable.gps_selector_unknown;
	}

}
